package cn.com.czpb.client.services;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.SPUtil;

/**
 * Created by Administrator on 2017/9/4.
 */

public class NetWorkChangeReceiver extends BroadcastReceiver {
    SPUtil spu;
    @Override
    public void onReceive(Context context, Intent intent) {
        spu=SPUtil.getInstance();
        ConnectivityManager connectionManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
//            if (networkInfo!=null&&networkInfo.isAvailable()){
//                Toast.makeText(context,"网络可用",Toast.LENGTH_SHORT).show();
//            }else {
//
//            }
        if (networkInfo!=null){
            if (networkInfo.isConnected()){
                if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    // connected to wifi
                    LogUtils.e("当前WiFi连接可用");
                    spu.setWifiStatus("wifi");
//                    Toast.makeText(context,"当前WiFi连接可用"+spu.getWifiStatus(),Toast.LENGTH_SHORT).show();

                } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    // connected to the mobile provider's data plan
                    LogUtils.e("当前数据连接可用");
                    spu.setWifiStatus("mobile");
//                    Toast.makeText(context,"当前数据连接可用",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
